<?php
namespace app\pay\controller\common;

use think\Controller;
use app\pay\controller\yop\Daifu as YopDaifu;
use app\pay\controller\yop\DaifuQuery as YopDaifuQuery;

use customtool\Utils;

class AdminDaifu extends Base {
    /*
     * 畅远后台管理向支付平台发起代付申请
     * @params  $id->model('PayDaifuApply')表中的id
     */
    public function apply() {

        $data = input('post.');
        $mod = array( // 必传参数
            0  => 'id',
            1  => 'is_agree',
        );
        $resParams = Utils::checkParamsExists($data, $mod, null);
        if($resParams) {
            return $this->sendApply($data); 
        } else {
            return $resParams;
        }
         
    }
    
    protected function sendApply($data) {
         // 根据商户号获取商户相关信息
        $resPayDaifuApply = model('PayDaifuApply')->get(['id' => $data['id']]);
        if($resPayDaifuApply) {
            if($resPayDaifuApply->is_agree == '0') {
                $resPayBis = model('PayBis')->get(['merchant_id' => $resPayDaifuApply->merchant_id]);
                if($data['is_agree'] == '1') {
                    return $this->applyPlatform($resPayBis, $resPayDaifuApply);
                } elseif($data['is_agree'] == '0') {
                    $res = payDaifuRefuse($resPayBis, $resPayDaifuApply); // 畅远后台拒绝代付请求
                    if($res) {
                        return retMsgTrue('订单已拒绝');
                    }
                }
            }
            return retMsgFalse('重复操作');
        } else {
            // 商户号不存在
            return paymchntIdFalse();
        }
    }
    
    /*
     *  条件选择代付申请平台
     */
    protected function applyPlatform($resPayBis, $resPayDaifuApply) {
        $resPayPlatform = model('PayPlatform')->get(['id' => $resPayBis->platform_id]);
        switch($resPayPlatform->platform) {
            case 'JuYin': // 聚银
                $result = json_encode('1', true);
            break;
            case 'FuMin': // 富民
                $result = json_encode('2', true);
            break;
            case 'Yop': // 易宝
                $YopDaifu = new YopDaifu();
                $result   = $YopDaifu->index($resPayBis, $resPayPlatform, $resPayDaifuApply);
            break;
        }
        return $result;
    }
    
    /*
     *  =========================================================我是最美分割线============================================
     */
    public function query() {
        $data = input('post.');
        $mod = array( // 必传参数
            0  => 'id',
        );
        $resParams = Utils::checkParamsExists($data, $mod, null);
        if($resParams) {
            return $this->sendQuery($data); 
        } else {
            return $resParams;
        }
         
    }
    
    protected function sendQuery($data) {
        $resPayDaifuApply = model('PayDaifuApply')->get(['id' => $data['id']]);
        if($resPayDaifuApply) {
            if($resPayDaifuApply->is_agree == '0') {
                return retMsgTrue('订单未审核');
            } elseif($resPayDaifuApply->is_agree == '2') {
                return retMsgTrue('订单已拒绝');
            } else {
                $resPayBis = model('PayBis')->get(['merchant_id' => $resPayDaifuApply->merchant_id]);
                return $this->queryPlatform($resPayBis, $resPayDaifuApply);
            }
        } else {
            return retMsgFalse('订单不存在');
        }
    }
    
    /*
     *  条件选择代付查询平台
     */
    protected function queryPlatform($resPayBis, $resPayDaifuApply) {
        $resPayPlatform = model('PayPlatform')->get(['id' => $resPayBis->platform_id]);
        switch($resPayPlatform->platform) {
            case 'JuYin': // 聚银
                $result = json_encode('1', true);
            break;
            case 'FuMin': // 富民
                $result = json_encode('2', true);
            break;
            case 'Yop': // 易宝
                $YopDaifuQuery = new YopDaifuQuery();
                $result        = $YopDaifuQuery->index($resPayBis, $resPayDaifuApply, $resPayPlatform);
            break;
        }
        return $result;
    }
}

